package com.xj.driver.mqtt.util;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author sunzhenzhen
 */


public class TimeUtil {
    public static long convertStringToLong(String dateString, String pattern) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = format.parse(dateString);
        return date.getTime();
    }

    /*获取现在的时间*/
    public static String getNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        return nowTime;
    }

    public static void main(String[] args) {
        try {
            long time = convertStringToLong("20240705100901950","yyyyMMddhhmmssSSS");
            System.out.println(time);
            //1720147290000
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = sdf.format(new Date(time)); // 将时间戳转换为字符串
            System.out.println(dateString); // 输出转换后的日期字符串
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
